package com.gdth.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * 
 * @author arvin
 *
 */
public class PhoneCallBroadcastReceiver extends BroadcastReceiver{ 
    private PhoneMediaRecorder recorder;
        
    public PhoneCallBroadcastReceiver() {
        recorder = new PhoneMediaRecorder();   
    }
        
    public PhoneCallBroadcastReceiver(PhoneMediaRecorder recorder) {
        this.recorder = recorder;    
    }
    
    @Override    
    public void onReceive(Context ctx, Intent intent) {    
        String phoneState = intent.getAction();    
         
        if (phoneState.equals(Intent.ACTION_NEW_OUTGOING_CALL)) {    
            String phoneNum = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            recorder.setNumber(phoneNum);    
            recorder.setIsCommingNumber(false);       
        }    
            
        if (phoneState.equals(PhoneCallState.DIALING)) {
        }    
            
        if (phoneState.equals(PhoneCallState.ALERTING)) {   
            recorder.setIsCommingNumber(true); 
        }    
            
        if (phoneState.equals(PhoneCallState.ACTIVE)) {    
            if (!recorder.isStarted()) {     
            	//if (!recorder.isCommingNumber() && !recorder.isStarted()) { 
                recorder.start();
            }    
        }    
            
        if (phoneState.equals(PhoneCallState.DISCONNECTED)) {    
            if (recorder.isStarted()) {    //!recorder.isCommingNumber() && recorder.isStarted()
                recorder.stop();
            }    
        }
    }

}
